package com.ruicar.afs.cloud.manage.finance.bankarrive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ValidOrInvalidEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.finance.bankarrive.condition.BankManagementCondition;
import com.ruicar.afs.cloud.manage.finance.bankarrive.condition.ReceiptRecordCondition;
import com.ruicar.afs.cloud.manage.finance.bankarrive.entity.ManageAccountRegister;
import com.ruicar.afs.cloud.manage.finance.bankarrive.entity.ManageBankAccount;
import com.ruicar.afs.cloud.manage.finance.bankarrive.service.ManageAccountRegisterService;
import com.ruicar.afs.cloud.manage.finance.bankarrive.service.ManageBankAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-07-09 11:40
 */
@Slf4j
@RestController
@Api("银行账户管理")
@RequestMapping("/bankManagement")
@AllArgsConstructor
public class BankManagementController {

    private final ManageBankAccountService manageBankAccountService;
    private final ManageAccountRegisterService manageAccountRegisterService;

    /**
     * 获取银行账号列表
     *
     * @param
     * @return
     * @author ZC.GUO
     */
    @PostMapping(value = "/queryBankList")
    @ApiOperation(value = "多条件分页获取银行账户信息")
    public IResponse<IPage<ManageBankAccount>> queryBankList(@ModelAttribute BankManagementCondition bankManagementCondition) {
        Page page = new Page(bankManagementCondition.getPageNumber(), bankManagementCondition.getPageSize());
        IPage<ManageBankAccount> manageBankAccountIPage = manageBankAccountService.page(page, Wrappers.<ManageBankAccount>query().lambda()
                .eq(EmptyUtils.isNotEmpty(bankManagementCondition.getAccount()), ManageBankAccount::getAccount, bankManagementCondition.getAccount())
                .eq(EmptyUtils.isNotEmpty(bankManagementCondition.getStatus()), ManageBankAccount::getStatus, bankManagementCondition.getStatus())
                .eq(EmptyUtils.isNotEmpty(bankManagementCondition.getBankCode()), ManageBankAccount::getBankCode, bankManagementCondition.getBankCode())
                .orderByAsc(ManageBankAccount::getStartTime)
        );
        return IResponse.success(manageBankAccountIPage);
    }


    /**
     * 银行账号生效失效
     *
     * @param
     * @return
     * @author ZC.GUO
     */
    @PostMapping(value = "/updateBankAccountStatus")
    @ApiOperation(value = "修改银行账户状态")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> updateBankAccountStatus(@ModelAttribute BankManagementCondition bankManagementCondition) {
        ManageBankAccount bankAccount = manageBankAccountService.getById(bankManagementCondition.getId());
        if (bankAccount != null) {
            /**  设为失效 by ZC.GUO  **/
            if (ValidOrInvalidEnum.invalid.equals(bankManagementCondition.getStatus())) {
                manageBankAccountService.update(Wrappers.<ManageBankAccount>update().lambda()
                        .eq(ManageBankAccount::getId, bankAccount.getId())
                        .set(ManageBankAccount::getStatus, ValidOrInvalidEnum.invalid)
                        .set(ManageBankAccount::getEndTime, Calendar.getInstance().getTime())
                );
                /** 设为有效 by ZC.GUO  **/
            } else {
                manageBankAccountService.update(Wrappers.<ManageBankAccount>update().lambda()
                        .eq(ManageBankAccount::getId, bankAccount.getId())
                        .set(ManageBankAccount::getStatus, ValidOrInvalidEnum.valid)
                        .set(ManageBankAccount::getEndTime, null)
                );
            }
            return IResponse.success("修改成功");
        } else {
            return IResponse.fail("修改失败");
        }
    }

    /**
     * 删除银行账户
     *
     * @param
     * @return
     * @author ZC.GUO
     */
    @PostMapping(value = "/deleteBankAccountStatus")
    @ApiOperation(value = "删除银行账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> deleteBankAccountStatus(@ModelAttribute BankManagementCondition bankManagementCondition) {
        ManageBankAccount bankAccount = manageBankAccountService.getById(bankManagementCondition.getId());
        if (bankAccount != null) {
            int count = manageAccountRegisterService.count(Wrappers.<ManageAccountRegister>query().lambda()
                    .eq(ManageAccountRegister::getBankAccount, bankAccount.getAccount())
            );
            if (count > 0) {
                return IResponse.fail("该账号已经被使用，不允许删除");
            } else {
                manageBankAccountService.removeById(bankAccount.getId());
                return IResponse.success("删除成功");
            }
        } else {
            return IResponse.fail("删除失败");
        }
    }

    /**
     * 添加银行账户
     *
     * @param
     * @return
     * @author ZC.GUO
     */
    @PostMapping(value = "/addSubmit")
    @ApiOperation(value = "添加银行账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> addSubmit(BankManagementCondition bankManagementCondition) {
        ManageBankAccount bankAccount = manageBankAccountService.getOne(Wrappers.<ManageBankAccount>query().lambda()
                .eq(ManageBankAccount::getAccount, bankManagementCondition.getAccount())
        );
        if (bankAccount != null) {
            return IResponse.fail("账号重复，添加失败");
        } else {
            bankAccount = new ManageBankAccount();
        }
        bankAccount.setBankCode(bankManagementCondition.getBankCode());
        bankAccount.setBranchBankName(bankManagementCondition.getBranchBankName());
        bankAccount.setEbankCode(bankManagementCondition.getEbankCode());
        bankAccount.setAccount(bankManagementCondition.getAccount());
        bankAccount.setStatus(ValidOrInvalidEnum.valid);
        bankAccount.setStartTime(Calendar.getInstance().getTime());
        manageBankAccountService.save(bankAccount);
        return IResponse.success("添加成功");
    }

    /**
     * 修改银行账户
     *
     * @param
     * @return
     * @author ZC.GUO
     */
    @PostMapping(value = "/editSubmit")
    @ApiOperation(value = "修改银行账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Object> editSubmit(BankManagementCondition bankManagementCondition) {
        ManageBankAccount bankAccount = manageBankAccountService.getOne(Wrappers.<ManageBankAccount>query().lambda()
                .eq(ManageBankAccount::getAccount, bankManagementCondition.getAccount())
                .ne(ManageBankAccount::getId, bankManagementCondition.getId())
        );
        if (bankAccount != null) {
            return IResponse.fail("账号重复，修改失败");
        } else {
            bankAccount = manageBankAccountService.getById(bankManagementCondition.getId());
            bankAccount.setBankCode(bankManagementCondition.getBankCode());
            bankAccount.setBranchBankName(bankManagementCondition.getBranchBankName());
            bankAccount.setEbankCode(bankManagementCondition.getEbankCode());
            bankAccount.setAccount(bankManagementCondition.getAccount());
            manageBankAccountService.updateById(bankAccount);
            return IResponse.success("编辑成功");
        }
    }

    @ApiOperation(value = "根据收款来源id查询来源表数据")
    @PostMapping("/getAccountRegister")
    public IResponse getAccountRegister(@RequestBody List<ReceiptRecordCondition> list) {
        for (ReceiptRecordCondition condition : list) {
            ManageAccountRegister accountRegister = manageAccountRegisterService.getById(condition.getSourceId());
            if (EmptyUtils.isNotEmpty(accountRegister)) {
                condition.setUserName(accountRegister.getPaymentCust());
                condition.setSummary(accountRegister.getSummary());
            }
        }
        return new IResponse().setData(list);
    }
}
